package com.zlt.dao.Impl;

import com.zlt.dao.ResumeDao;
import com.zlt.entity.Delivery;
import com.zlt.entity.OnlineResume;
import com.zlt.entity.Resume;
import com.zlt.entity.User;
import com.zlt.util.SqlUtil;

import java.util.List;

public class ResumeDaoImpl implements ResumeDao {
    @Override
    public Integer insertOnlineResume(OnlineResume onlineResume) {
        return SqlUtil.insert(onlineResume);
    }

    @Override
    public List<Resume> selectAllResumeByUid(String uid) {
        String sql = "select rid,uid,path,filename,create_time,update_time,state from resume where uid = ? and state in(0,1)";
        return SqlUtil.select(Resume.class, sql, uid);
    }

    @Override
    public Integer deleteResumeByRid(Integer rid) {
        String sql = "update resume set state = 2 where rid = ?";
        return SqlUtil.update(sql, rid);
    }

    @Override
    public Resume selectResumePathByRid(Integer rid) {
        String sql = "select path from resume where rid = ?";
        return SqlUtil.selectOne(Resume.class, sql, rid);
    }

    @Override
    public Integer updateResumeState(Integer rid) {
        String sql = "update state = 2 from resume where rid = ?";
        return SqlUtil.update(sql, rid);
    }

    @Override
    public Integer updateDefaultResumeStateToNormal() {
        String sql = "update resume set state = 0 where state = ?";
        return SqlUtil.update(sql,1);
    }

    @Override
    public Integer updateDefaultResumeByRid(Integer rid) {
        String sql = "update resume set state = 1 where rid = ?";
        return SqlUtil.update(sql, rid);
    }

    @Override
    public Integer updateSalaryByUid(String minSalary, String maxSalary, String uid) {
        String sql = "update online_resume set min_salary = ? , max_salary = ? where uid = ?";
        return SqlUtil.update(sql, minSalary, maxSalary , uid);
    }

    @Override
    public Integer insertResume(Resume resume) {
        return SqlUtil.insert(resume);
    }

    @Override
    public User getUSerByUid(String uid) {
        String sql = "select uid,username,password,nickname,reg_time,log_time,sex,salt,email,birthday,state,photo from user where uid = ?";
        return SqlUtil.selectOne(User.class,sql,uid);
    }

    @Override
    public int updatePhoto(String uid, String url) {
        String sql = "update user set photo = ? where uid = ?";
        return SqlUtil.update(sql,url,uid);
    }

    @Override
    public Resume selectDefaultResume() {
        String sql = "select rid,uid,path,filename,create_time,update_time,state from resume where state = 1";
        return SqlUtil.selectOne(Resume.class,sql);
    }

    @Override
    public int insertDelivery(Delivery delivery) {
        return SqlUtil.insert(delivery);
    }

    @Override
    public Integer updateAdvantageByUid(String advantage, String uid) {
        String sql = "update online_resume set advantage = ? where uid = ?";
        return SqlUtil.update(sql,advantage,uid);
    }

    @Override
    public Integer updateExpectedPositionByUid(String expectedPosition, String uid) {
        String sql = "update online_resume set expected_position = ? where uid = ?";
        return SqlUtil.update(sql,expectedPosition,uid);
    }

    @Override
    public Integer updateEducationByUid(String education, String uid) {
        String sql = "update online_resume set education = ? where uid = ?";
        return SqlUtil.update(sql,education,uid);
    }

    @Override
    public Integer updateInternshipByUid(String internship, String uid) {
        String sql = "update online_resume set internship = ? where uid = ?";
        return SqlUtil.update(sql,internship,uid);
    }

    @Override
    public Integer updateSalary(String minSalary, String maxSalary, String uid) {
        String sql = "update online_resume set min_salary = ? , max_salary = ? where uid = ?";
        return SqlUtil.update(sql,minSalary,maxSalary,uid);
    }

    @Override
    public OnlineResume selectOnlineResumeByUid(String uid) {
        String sql = "select online_resume.id ,online_resume.uid ,online_resume.advantage, online_resume.expected_position" +
                ",online_resume.education,online_resume.internship,online_resume.min_salary,online_resume.max_salary from online_resume where uid = ?";
        return SqlUtil.selectOne(OnlineResume.class,sql,uid);
    }
}
